package com.sj.purchase.modules.cost.web;

import com.sj.purchase.common.utils.FileOperateUtil;
import com.sj.purchase.modules.cost.repository.stock.StockCostRepository;
import com.sj.purchase.modules.cost.domain.stock.StockCost;
import com.sj.purchase.util.DateUtils;
import com.sj.purchase.util.excel.ExcelUtil;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * Created by 宋开宗 on 2017/7/21 0021.
 */
@Deprecated
@RestController
@RequestMapping("/stock/cost")
public class StockCostController {
	@Autowired
	private StockCostRepository repository;

	/**
	 * 根据月份查询
	 * @param month
	 * @param pageable
	 * @return
	 */
	@GetMapping
	public ResponseEntity list(@DateTimeFormat(pattern = "yyyy-MM") Date month,
							   @PageableDefault(sort = {"createDate"}) Pageable pageable) {
		return ResponseEntity.ok(repository.findByCreateDateBetween(month, DateUtils.getLastDayOfMonth(month), pageable));
	}

	/**
	 * 导出Excel
	 * @param month
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@GetMapping("/excel")
	public void exportShSalesExcel(@DateTimeFormat(pattern = "yyyy-MM") Date month,
								   HttpServletRequest request,
								   HttpServletResponse response) throws Exception {
		Page<StockCost> pages = repository.findByCreateDateBetween(month, DateUtils.getLastDayOfMonth(month), null);
		FileOperateUtil.download(request, response, DateFormatUtils.format(month, "yyyy-MM") + "期末库存成本.xls", new ExcelUtil<StockCost>(StockCost.class).exportExcel(pages.getContent(), "期末库存成本"));
	}
}
